import request from '@/utils/request';
import {download, toFormData, checkDownloadRes} from '@/utils/common';

/**
 * 分页查询报表
 */
export async function pageReports(params) {
  const res = await request.get('/admin/jimu/report/list', {params});
  if (res.data.code === 200) {
    return res.data;
  }
  return Promise.reject(new Error(res.data.msg));
}

/**
 * 查询全部报表
 */
export async function listReports(params) {
  const res = await request.get('/admin/jimu/report/list', {params});
  if (res.data.code === 200) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.msg));
}

/**
 * 根据id查询报表
 */
export async function getReport(id) {
  const res = await request.get('/admin/jimu/report/report/' + id);
  if (res.data.code === 200) {
    return res.data;
  }
  return Promise.reject(new Error(res.data.msg));
}

/**
 * 添加报表
 */
export async function addReport(data) {
  const res = await request.post('/admin/jimu/report', data);
  if (res.data.code === 200) {
    return res.data.msg;
  }
  return Promise.reject(new Error(res.data.msg));
}

/**
 * 修改报表
 */
export async function updateReport(data) {
  const res = await request.put('/admin/jimu/report', data);
  if (res.data.code === 200) {
    return res.data.msg;
  }
  return Promise.reject(new Error(res.data.msg));
}

/**
 * 删除报表
 */
export async function removeReport(id) {
  const res = await request.delete('/admin/jimu/report/' + id);
  if (res.data.code === 200) {
    return res.data.msg;
  }
  return Promise.reject(new Error(res.data.msg));
}

/**
 * 批量删除报表
 */
export async function removeReports(ids) {
  const res = await request.delete('/admin/jimu/report/' + ids.join());
  if (res.data.code === 200) {
    return res.data.msg;
  }
  return Promise.reject(new Error(res.data.msg));
}

/**
 * 导出报表
 */
export async function exportReports(params) {
  const res = await request({
    url: '/admin/jimu/report/export',
    method: 'POST',
    data: toFormData(params),
    responseType: 'blob'
  });
  await checkDownloadRes(res);
  download(res.data, `report_${Date.now()}.xlsx`);
}


/**
 * 修改链接状态
 */
export async function updateReportStatus(id, status) {
  const res = await request.put('/admin/jimu/report/changeStatus', {
    id,
    status
  });
  if (res.data.code === 200) {
    return res.data.msg;
  }
  return Promise.reject(new Error(res.data.msg));
}
